package com.nf.health.app.activity.global;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.VerticalSlideRuler;
import com.nf.health.app.utils.LogUtil;
import com.nf.health.app.utils.PreferenceHelper;

/** 
* @ClassName: RegisterHeightActivity 
* @Description: 注册 身高
* @author Ian 
* @date 2015-11-4 上午11:02:01 
*  
*/
public class RegisterHeightActivity extends BaseActivity {

	private Button next_btn;
	private String sex;
	private ImageView sex_img;

	// private LinearLayout staff_linear;

	private TextView titlebar_title_tv;

	private String nick_name;

	private String type = "";
	private ScrollView sv;
	private VerticalSlideRuler v_slide_ruler;
	private int height = 170;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_register_height);
		type = getIntent().getStringExtra("type");
		initView();
		initListener();
	}

	public void initView() {
		titlebar_title_tv = (TextView) this.findViewById(R.id.titlebar_title_tv);
		titlebar_title_tv.setText("身高");
		sex_img = (ImageView) this.findViewById(R.id.sex_img);
		next_btn = (Button) this.findViewById(R.id.next_btn);
		if ("1".equals(type)) {
			double doubleH = Double.parseDouble(PreferenceHelper.getString(PreferenceHelper.USERHEIGHT, "170"));
			height = (int) doubleH;
			sex = PreferenceHelper.getString(PreferenceHelper.USERSEX, "1");
			next_btn.setText("确定");
		} else {
			sex = getIntent().getStringExtra("sex");
		}

		nick_name = getIntent().getStringExtra("nick_name");
		if ("1".equals(sex)) {
			sex_img.setBackgroundResource(R.drawable.ka2);
		} else {
			sex_img.setBackgroundResource(R.drawable.ka);
		}
		sv = (ScrollView) findViewById(R.id.sv);
		v_slide_ruler = (VerticalSlideRuler) findViewById(R.id.v_slide_ruler);

		next_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ("1".equals(type)) {
					provider.requestUpdateUser(null, null, null, null, height+ "", null,null,null,null, "updataUser");
				} else {
					Intent intent = new Intent(RegisterHeightActivity.this, RegisterWeightActivity.class);
					intent.putExtra("sex", sex);
					intent.putExtra("nick_name", nick_name);
					intent.putExtra("height", height + "");
					intent.putExtra("type",type);
					startActivity(intent);
				}
			}
		});
		
		sv.post(new Runnable() {
			
			@Override
			public void run() {
				setValue(height);
			}

			private void setValue(int defaultValue) {
				sv.smoothScrollTo(
						sv.getScrollX(),
						(int) ((v_slide_ruler.getTotalCount()-height+v_slide_ruler.getStartValue())
								* v_slide_ruler.getUnitWidth()
								- sv.getHeight() / 2 + v_slide_ruler
								.getDelay()));
			}
		});

	}

	private void initListener() {
		sv.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_UP:
					final float halfHeight = (float) sv.getHeight() / 2;

					float value = 0;// 计算滚动的块数

					value = (halfHeight + sv.getScrollY() - v_slide_ruler.getDelay()) / v_slide_ruler.getUnitWidth();
					if ((value - (int) value) > 0.5)
						value = (int) (value + 1);
					else
						value = (int) (value);
						final int val = (int) value;

						v.post(new Runnable() {

							@Override
							public void run() {
								sv.smoothScrollTo(
										sv.getScrollX(),
										(int) (val * v_slide_ruler.getUnitWidth()
												- halfHeight + v_slide_ruler
												.getDelay()));
								height = v_slide_ruler.getTotalCount()-val+v_slide_ruler.getStartValue();
//								height = val + v_slide_ruler.getStartValue();
								LogUtil.i("TAG", height+">>>>"+val+">>>>>"+v_slide_ruler.getStartValue());
							}
						});
//					}

					break;
				}
				return false;
			}
		});
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("updataUser")) {
			showToast("修改成功!");
			finish();
			PreferenceHelper.putString(PreferenceHelper.USERHEIGHT, height+"");
		}
	}
}
